package com.itheima.controller;

import com.itheima.MessageConstant;
import com.itheima.pojo.Blog;
import com.itheima.pojo.Tag;
import com.itheima.service.BlogService;
import com.itheima.service.TagService;
import com.itheima.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 博客标签相关接口
 */
@RestController
@RequestMapping("/tag")
@Api(tags = "博客标签相关接口")
public class TagController {

    @Autowired
    BlogService blogService;

    @Autowired
    TagService tagService;

    @ApiOperation("通过TagUid获取文章")
    @GetMapping("/getArticleByTagUid/{tagUid}/{currentPage}/{pageSize}")
    public String getArticleByTagUid(@PathVariable String tagUid,@PathVariable Long currentPage,@PathVariable Long pageSize){
        if (tagUid==null||"".equals(tagUid)){
            return ResultUtil.result(HttpServletResponse.SC_BAD_REQUEST,"请输入标签");
        }
        try {
            List<Blog> blogList=blogService.getArticleByTagUid(tagUid,currentPage,pageSize);
            return ResultUtil.result(HttpServletResponse.SC_OK,blogList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(HttpServletResponse.SC_BAD_REQUEST, MessageConstant.GET_ARTICLE_BY_TAGUID_FAIL);
        }
    }

    @ApiOperation("获取标签的信息")
    @GetMapping("/getTagList")
    public String getArticleByTagUid(){
        try {
            List<Tag> blogList=tagService.getTagList();
            return ResultUtil.result(HttpServletResponse.SC_OK,blogList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(HttpServletResponse.SC_BAD_REQUEST, MessageConstant.TAG_FINDALL_FAIL);
        }
    }
}
